#include "xlcd.h"
#include "delay.h"

/*******************************************************************
Function Name:          OpenXLCD
Description:            This function configure the IO pins  
                        and initializes the LCD controller
                        the lcdtype should contain data on the type 
                        of interface and number of lines of display.
Input parameters:       char lcdtype 
Return value:           none
********************************************************************/
void OpenXLCD(char lcdType)
{

    RW_PIN = 0;
    RS_PIN = 0;
    E_PIN = 0;

    /*Initialize the data port/control pins to zero */
    DATA_PIN_7 = 0;
    DATA_PIN_6 = 0;
    DATA_PIN_5 = 0;
    DATA_PIN_4 = 0;

    /* Configure the data pins as digital outputs */

    TRIS_DATA_PIN_7 = 0;
    TRIS_DATA_PIN_6 = 0;
    TRIS_DATA_PIN_5 = 0;
    TRIS_DATA_PIN_4 = 0;

    AN_PIN_7 = 0;
    AN_PIN_6 = 0;
    AN_PIN_5 = 0;
    AN_PIN_4 = 0;

    /* Make all control pins as digital */
    AN_RS = 0;

    TRIS_RW = 0;
    TRIS_RS = 0;
    TRIS_E = 0;

    DelayPORXLCD();/* Allow a delay for POR.(minimum of 40ms) */

    WriteCmdXLCD(EIGHT_BIT);

    DelayXLCD(); /*Wait for more than 39 us*/

    WriteCmdXLCD(lcdType);

    /* wait for command completion */
    while(BusyXLCD());

    /* Display ON/OFF CONTROL*/
    WriteCmdXLCD(DON & BLINK_OFF & CURSOR_ON);

    /* wait for command completion */
    while(BusyXLCD());

    /* LCD Clear	*/
    WriteCmdXLCD(CLEAR_XLCD);

    /* wait for command completion */
    while(BusyXLCD());

    /*Entry Mode Set	*/
    WriteCmdXLCD(ENTIRE_SHIFT_OFF & INCR_MODE);

    /* wait for command completion */
    while(BusyXLCD());

   } /* end of function */
